package com.xawl.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.xawl.pojo.Message;
import com.xawl.pojo.User;
import com.xawl.service.MessageService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/message")
public class MessageController {

    protected final MessageService messageService;

    public MessageController(@Qualifier("messageServiceImpl") MessageService messageService) {
        this.messageService = messageService;
    }

    // 查询所有留言
    @RequestMapping("/queryAllMessage")
    @ResponseBody
    public String queryAllMessage()  {
        List<Message> messageList = messageService.queryAll();
        ObjectMapper mapper=new ObjectMapper();
        String jsonStr = null;
        try {
            jsonStr = mapper.writeValueAsString(messageList);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return jsonStr;
    }

    // 发表留言
    @RequestMapping("/publishMessage")
    @ResponseBody
    public String publishMessage(String  content,HttpServletRequest req){
        Message message = new Message();
        //获取当前用户
        User user = (User) req.getSession().getAttribute("user");
        message.setMsgContent(content);
        message.setMsgUsername(user.getUsername());
        message.setMsgUserFace(user.getFace());
        message.setMsgDate(new Date());
        messageService.addMessage(message);
        List<Message> messageList = new ArrayList<Message>();
        messageList.add(message);
        ObjectMapper mapper=new ObjectMapper();
        String jsonStr = null;
        try {
            jsonStr = mapper.writeValueAsString(messageList);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return jsonStr;
    }

    /* 获取数据列表 */
    @ResponseBody
    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    public JSONObject getList(int page, int limit) {
        PageHelper.startPage(page, limit);
        List<Message> list = messageService.queryAll();
        Integer total = messageService.getTotal();
        JSONObject json = new JSONObject();
        json.put("code", 0);
        json.put("msg", 1);
        json.put("count", total);
        json.put("data", list);
        return json;
    }

    /* 增加 */
    @RequestMapping("/add")
    public String add() {
        return "red-website-message/red-website-message-edit";
    }

    @RequestMapping("/addData")
    @ResponseBody
    public String addMessage(@RequestBody Message message) {
        int i = messageService.addMessage(message);
        return i != 0 ? "ok" : "error";
    }

    @RequestMapping("/edit/{id}")
    public String edit(Model model, @PathVariable("id") int id) {
        model.addAttribute("mId", id);
        return "red-website-message/red-website-message-edit";
    }

    /* 删除 */
    @RequestMapping("/delete")
    @ResponseBody
    public String delete(@RequestBody int id) {
        messageService.deleteMessage(id);
        return "ok";
    }

    /* 删除多条记录 */
    @RequestMapping("/deletes")
    @ResponseBody
    public String deletes(@RequestBody Long[] ids) {
        int i = messageService.deleteMessages(ids);
        return i != 0 ? "ok" : "error";
    }

    /* 更新 */
    @RequestMapping("/update")
    @ResponseBody
    public String update(@RequestBody Message message) {
        int i = messageService.updateMessage(message);
        return i != 0 ? "ok" : "error";
    }

    /* 查看 */
    @RequestMapping("/view/{id}")
    public String view(Model model, @PathVariable("id") int id) {
        model.addAttribute("mId", id);
        return "red-website-message/red-website-message-view";
    }

    /* 数据回显 */
    @RequestMapping("/viewData")
    @ResponseBody
    public Message viewData(@RequestBody int id) {
        return messageService.findMessageById(id);
    }

}
